package cn.fintecher.pangolin.service.manage.web;

import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.elastic.Strategy;
import cn.fintecher.pangolin.service.manage.request.AddStrategyRequest;
import cn.fintecher.pangolin.service.manage.request.CaseStrategyPreviewRequest;
import cn.fintecher.pangolin.service.manage.request.StrategyPreviewRequest;
import cn.fintecher.pangolin.service.manage.response.StrategyPreviewResponse;
import cn.fintecher.pangolin.service.manage.response.StrategyResponse;
import cn.fintecher.pangolin.service.manage.service.StrategyService;
import io.swagger.annotations.*;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Auther: xiaqun
 * @Description: 策略相关接口
 * @Date: 16:02 2019/8/30
 */

@RestController
@RequestMapping("/api/strategyController")
@Api(value = "策略相关接口", description = "策略相关接口")
public class StrategyController extends BaseController {
    @Autowired
    private StrategyService strategyService;

    /**
     * 多条件查询分案策略
     */
    @ApiOperation(value = "多条件查询分案策略", notes = "多条件查询分案策略")
    @GetMapping("/findAllStrategy")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<StrategyResponse>> findAllStrategy(@RequestParam(required = false) @ApiParam(value = "策略名称") String name,
                                                                  Pageable pageable) {
        log.info("REST request to find all strategy,{}", name);
        Page<StrategyResponse> page = strategyService.findAllStrategy(name, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 新增策略
     */
    @ApiOperation(value = "新增策略", notes = "新增策略")
    @PostMapping("/addStrategy")
    public ResponseEntity<Void> addStrategy(@Valid @RequestBody AddStrategyRequest request,
                                            @RequestHeader(value = "X-UserToken") String token) throws JSONException {
        log.info("REST request to add strategy,{}", request);
        strategyService.addStrategy(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除策略
     */
    @ApiOperation(value = "删除策略", notes = "删除策略")
    @DeleteMapping("/deleteStrategy")
    public ResponseEntity<Void> deleteStrategy(@RequestParam @ApiParam(value = "策略ID", required = true) String id) {
        log.info("REST request to delete strategy,{}", id);
        strategyService.deleteStrategy(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改策略
     */
    @ApiOperation(value = "修改策略", notes = "修改策略")
    @PutMapping("/modifyStrategy")
    public ResponseEntity<Void> modifyStrategy(@Valid @RequestBody AddStrategyRequest request,
                                               @RequestHeader(value = "X-UserToken") String token) throws JSONException {
        log.info("REST request to modify strategy,{}", request);
        strategyService.modifyStrategy(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取策略对象
     */
    @ApiOperation(value = "获取策略对象", notes = "获取策略对象")
    @GetMapping("/getStrategy")
    public ResponseEntity<Strategy> getStrategy(@RequestParam @ApiParam(value = "主键ID", required = true) String id) {
        log.info("REST request to get strategy,{}", id);
        Strategy strategy = strategyService.getStrategy(id);
        return ResponseEntity.ok().body(strategy);
    }

    /**
     * 待分配策略预览
     */
    @ApiOperation(value = "待分配策略预览", notes = "待分配策略预览")
    @GetMapping("/strategyPreview")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<StrategyPreviewResponse>> strategyPreview(StrategyPreviewRequest request,
                                                                         Pageable pageable) throws Exception {
        log.info("REST request to strategy preview,{}", request);
        Page<StrategyPreviewResponse> page = strategyService.strategyPreview(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 案件池策略预览
     */
    @ApiOperation(value = "案件池策略预览", notes = "案件池策略预览")
    @GetMapping("/caseStrategyPreview")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<StrategyPreviewResponse>> caseStrategyPreview(CaseStrategyPreviewRequest request,
                                                                             Pageable pageable) throws Exception {
        log.info("REST request to case strategy preview,{}", request);
        Page<StrategyPreviewResponse> page = strategyService.caseStrategyPreview(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 复制策略
     */
    @ApiOperation(value = "复制策略", notes = "复制策略")
    @GetMapping("/copyStrategy")
    public ResponseEntity<Void> copyStrategy(@RequestParam @ApiParam(value = "策略ID", required = true) String id,
                                             @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to copy strategy,{}", id);
        strategyService.copyStrategy(id, token);
        return ResponseEntity.ok().build();
    }
}